package cn.hxzy.icon.icon.service.impl;


import cn.hxzy.icon.icon.entity.IconGroup;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.repository.IconGroupRepository;
import cn.hxzy.icon.icon.service.IconGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 实现
 *
 * @author little black
 */
@Service("IconGroupService")
public class IconGroupServiceImpl implements IconGroupService {
    @Autowired
    private IconGroupRepository iconGroupRepository;

    /**
     * 查询全部
     *
     * @param pageBean
     * @return
     */
    @Override
    public PageBean<IconGroup> findAll(PageBean<IconGroup> pageBean) {
        Pageable pageable = PageRequest.of(pageBean.getPage() - 1, pageBean.getSize(), Sort.by(Sort.Order.asc("id")));
        Page<IconGroup> all = iconGroupRepository.findAll(pageable);
        pageBean.setList(all.getContent());
        pageBean.setCount(all.getTotalElements());
        pageBean.setPageCount(all.getTotalPages());
        return pageBean;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        iconGroupRepository.deleteById(id);
    }

    /**
     * 添加
     *
     * @param iconGroup
     */
    @Override
    public void add(IconGroup iconGroup) {
        iconGroupRepository.save(iconGroup);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @Override
    public IconGroup FindById(Integer id) {
        Optional<IconGroup> byId = iconGroupRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    /**
     * 修改
     *
     * @param iconGroup
     */
    @Override
    public void updeat(IconGroup iconGroup) {
        iconGroupRepository.save(iconGroup);
    }

    @Override
    public List<IconGroup> findAll1() {
        List<IconGroup> all = iconGroupRepository.findAll();
        return all;
    }
}
